import 'package:enjoy_plan/page/util/ui_adapt.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

import '../data/plan.dart';
import '../locale/app_localizations.dart';

/// @author: lijinxi.1220@bytedance.com
/// @description:
/// @date: 2024/11/15 19:16

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  final dynamic userName = "lijinxi.1220";

  @override
  Widget build(BuildContext context) {
    //final LoginData loginData =
    //  ModalRoute.of(context)?.settings.arguments as LoginData;
    return LayoutBuilder(
        builder: (BuildContext ctx, BoxConstraints constraints) {
      dynamic isLarge = constraints.maxWidth > UIAdapt.minWidth;
      return Container(
          color: Colors.white,
          child: Column(mainAxisSize: MainAxisSize.max, children: [
            TopBarWidget(isLarge: isLarge),
            Padding(
              padding: EdgeInsets.only(top: 32, bottom: 32),
              child: Text(
                userName + "'s Study Plan",
                style: TextStyle(
                    fontSize: 32,
                    fontWeight: FontWeight.w700,
                    color: Colors.black,
                    decoration: TextDecoration.none),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 64, right: 64, bottom: 16),
              child: Row(
                children: [
                  TextButton(
                      style: TextButton.styleFrom(
                          elevation: 10,
                          backgroundColor: Colors.grey,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8.0),
                          )),
                      onPressed: () {},
                      child: Text(
                        AppLocalizations.of(context).filterByDate,
                        style: TextStyle(color: Colors.black),
                      )),
                  SizedBox(
                    width: 8,
                  ),
                  TextButton(
                      style: TextButton.styleFrom(
                          elevation: 10,
                          backgroundColor: Colors.grey,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8.0),
                          )),
                      onPressed: () {},
                      child: Text(
                        AppLocalizations.of(context).filterByDate,
                        style: TextStyle(color: Colors.black),
                      )),
                  SizedBox(
                    width: 8,
                  ),
                  Icon(
                    FontAwesomeIcons.list,
                    color: Colors.black,
                    size: 20,
                  ),
                  Spacer(),
                  TextButton(
                      onPressed: () {},
                      style: TextButton.styleFrom(
                          backgroundColor: Colors.blueAccent,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(20.0),
                          )),
                      child: Text.rich(TextSpan(
                          style: TextStyle(color: Colors.black54, fontSize: 13),
                          children: [
                            WidgetSpan(
                              child: Icon(
                                FontAwesomeIcons.plus,
                                color: Colors.white,
                                size: 20,
                              ),
                              alignment: PlaceholderAlignment.bottom,
                            ),
                            TextSpan(
                              text: AppLocalizations.of(context).addNewPlan,
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.w700,
                                  fontSize: 18),
                            ),
                          ]))),
                ],
              ),
            ),
            ListWidget(plans: generateRandomPlans())
          ]));
    });
  }
}

class ListWidget extends StatelessWidget {
  final List<Plan> plans;

  const ListWidget({super.key, required this.plans});

  @override
  Widget build(BuildContext context) {
    return Expanded(
        child: Container(
            padding: EdgeInsets.only(left: 64, right: 64),
            child: GridView.builder(
              shrinkWrap: true,
              gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                  mainAxisExtent: 300,
                  maxCrossAxisExtent: 400,
                  mainAxisSpacing: 16,
                  crossAxisSpacing: 16),
              itemBuilder: (context, index) {
                return ListItemWidget(plan: plans[index]);
              },
              itemCount: plans.length,
            )));
  }
}

class ListItemWidget extends StatelessWidget {
  final Plan plan;

  const ListItemWidget({super.key, required this.plan});

  @override
  Widget build(BuildContext context) {
    return Card(
        color: Colors.white54,
        child: SizedBox(
          width: 400,
          height: 200,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ClipRRect(
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(16),
                    topRight: Radius.circular(16)),
                child: Image.network(
                  width: 400,
                  height: 200,
                  fit: BoxFit.fill,
                  plan.cover,
                ),
              ),
              SizedBox(
                height: 8,
              ),
              Padding(
                  padding: EdgeInsets.only(left: 8, right: 8),
                  child: Text(
                    plan.name,
                    style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                        color: Colors.black,
                        decoration: TextDecoration.none),
                  )),
              SizedBox(
                height: 8,
              ),
              Padding(
                  padding: EdgeInsets.only(left: 8, right: 8),
                  child: Text(
                    plan.description,
                    maxLines: 3,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        fontSize: 12,
                        fontWeight: FontWeight.w500,
                        color: Colors.black,
                        decoration: TextDecoration.none),
                  )),
            ],
          ),
        ));
  }
}

class TopBarWidget extends StatelessWidget {
  final bool isLarge;

  const TopBarWidget({
    super.key,
    required this.isLarge,
  });

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Container(
            alignment: Alignment.center,
            color: Colors.blueAccent,
            padding: EdgeInsets.only(top: 8, left: 16, right: 16, bottom: 8),
            child: Stack(alignment: Alignment.center, children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                mainAxisSize: MainAxisSize.max,
                children: [
                  Icon(
                    FontAwesomeIcons.bookOpen,
                    color: Colors.white,
                  ),
                  SizedBox(
                    width: 16,
                  ),
                  Text(
                    AppLocalizations.of(context).title,
                    style: TextStyle(
                        fontSize: UIAdapt.fontSize(large: isLarge, size: 24),
                        fontWeight: FontWeight.w500,
                        decoration: TextDecoration.none,
                        color: Colors.white),
                  ),
                  Spacer(),
                  SizedBox(
                    height: 32,
                    width: 32,
                    child: Icon(
                      FontAwesomeIcons.gear,
                      color: Colors.white,
                    ),
                  )
                ],
              ),
              Row(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  SizedBox(
                    width: 24,
                    height: 24,
                    child: Icon(
                      size: 16,
                      FontAwesomeIcons.boltLightning,
                      color: Colors.yellow,
                    ),
                  ),
                  SizedBox(
                    width: 4,
                  ),
                  Text(
                    AppLocalizations.of(context).proVersion,
                    style: TextStyle(
                        fontWeight: FontWeight.w700,
                        color: Colors.yellow,
                        decoration: TextDecoration.none,
                        fontSize: UIAdapt.fontSize(large: isLarge, size: 20)),
                  ),
                  SizedBox(
                    width: 4,
                  ),
                  Padding(
                      padding: EdgeInsets.only(left: 8),
                      child: SizedBox(
                          width: 24,
                          height: 24,
                          child: Icon(FontAwesomeIcons.circleExclamation,
                              size: 16, color: Colors.white))),
                ],
              ),
            ])));
  }
}
